<?
    // this is a peculiar little file
    // it parses through text and replaces it with values
    // basically lets you use php vars in plain text
    
    // Places where DynaText is used (examples):
    //   * When parsing the email for guest account confirmations
    
    // more documentation later once I figure out how I can actually use this practically...
    
    class DynaText {
        private static $_custom = array();                  // custom pattern matches and replacements
        
        static function AddCustom($search,$replace) {
            self::$_custom[$search] = $replace;
        }
        
        // do any custom replacements that were specified.
        // this is if you need to bind to a value that was computed
        // rather than one that was pulled from a DB or array
        static function DoCustomReplacements($str) {
            foreach (self::$_custom as $search => $replace) {
                $str = str_replace($search,$replace,$str);
            }
            return $str;
        }
        
        private static function matchSymbol($str,$symbol) {
            $matches = array();
            preg_match_all("/$symbol(\w+)$symbol/",$str,$matches);
            return $matches[1];
        }
        private static function replaceAll($str,$symbol,$arr) {
            foreach (self::matchSymbol($str,$symbol) as $find) {
                $str = str_replace($symbol.$find.$symbol,$arr[$find],$str);
            }
            return $str;
        }
        
        static function BindServerVars($str,$symbol = "%") {
            $str = self::DoCustomReplacements($str);
            return self::replaceAll($str,$symbol,$_SERVER);
        }
        
        static function BindFromDatabase($str,$table,$rowid,$symbol = "#") {
            $str = self::DoCustomReplacements($str);
            $query = "SELECT * FROM $table WHERE id=$rowid;";
            $data = DBM::FetchRow($query);
            return self::replaceAll($str,$symbol,$data);
        }
        
        static function BindSettings($str,$symbol = "@") {
            $str = self::DoCustomReplacements($str);
            $settings = SettingManager::GetSettings();
            return self::replaceAll($str,$symbol,$settings);
        }
        
        static function ResolveLinks($str, $symbol = "!") {
            $matches = array();
            preg_match_all("/!(\w+)!/",$str,$matches);
            $matches = $matches[1];
            
            foreach ($matches as $match) {
                $parts = explode("?",$match);
                $page = $parts[0];
                $querystring = null;
                
                if ($parts[1]) $querystring = "?" . $parts[1];
                $replacement = Conf::GetApplicationRoot() . "/$page.php" . ($querystring ? $querystring : "");
                $str = str_replace("!$match!",$replacement,$str);
            }
            
            return $str;
        }
        
        static function BindFromEnv($str) {
            $str = self::DoCustomReplacements($str);
            $str = self::BindServerVars($str);
            $str = self::BindSettings($str);
            $str = self::ResolveLinks($str);
            $str = self::replaceAll($str,"%",$_ENV);
            return $str;
        }
        
        static function ParseForHTML($str) {
            return str_replace(array("<br>","&nbsp;&nbsp;&nbsp;&nbsp;"),array("\n","\t"),$str);
        }
        static function ParseForMail($str) {
            return str_replace(array("\n","\t"),array("<br>","&nbsp;&nbsp;&nbsp;&nbsp;"),$str);
        }
    }
?>